package com.yc.ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.LoginIDao;
import com.yc.dao.LoginDao;
import com.yc.utils.DataDict;
import com.yc.utils.MailUtils;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;

public class Login extends Composite {
	private static Text text_1;
	private Text text_2;
	private Text text_3;
	private Text text_4;
	private static Text text_5;
	private static Text text_6;
	private Text text_7;
	private static Text text_8;

	private Map<String, Object> map;

	static String l_id; // 住宿编号
	private Label label_3;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Login(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));

		map = MainUI.map;

		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackgroundImage( SWTResourceManager.getImage(MainUI.class, "/com/yc/images/登记入住2.PNG") );
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		SWTUtil.imagetSize(composite);

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setText("房间号：");
		lblNewLabel.setBounds(71, 122, 98, 25);

		text_1 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_1.setBounds(187, 119, 173, 26);

		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setBounds(71, 174, 76, 20);
		lblNewLabel_2.setText("身份证号：");

		text_2 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_2.setBounds(187, 171, 173, 26);
		text_2.setText(map.get("c_cardid").toString());

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setImage(SWTResourceManager.getImage(Login.class, "/com/yc/images/登记入住界面按钮3.PNG"));
		btnNewButton.setBounds(756, 541, 98, 30);
		btnNewButton.setText("打印登记表");

		Label lblNewLabel_3 = new Label(composite, SWT.NONE);
		lblNewLabel_3.setBounds(71, 67, 76, 20);
		lblNewLabel_3.setText("姓名：");

		text_3 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_3.setBounds(187, 67, 173, 26);
		text_3.setText(map.get("c_name").toString());

		Date date = new Date(System.currentTimeMillis()); // 当前日期
		String dateString = String.format("%tF", date);

		Label label = new Label(composite, SWT.NONE);
		label.setBounds(620, 67, 76, 20);
		label.setText("入住日期：");

		text_4 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_4.setBounds(737, 64, 139, 26);
		text_4.setText(dateString);

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setBounds(620, 122, 76, 20);
		label_1.setText("房间类型");

		text_5 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_5.setBounds(737, 119, 139, 26);

		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setBounds(620, 177, 76, 20);
		label_2.setText("房间价格：");

		text_6 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_6.setBounds(737, 171, 139, 26);

		Label lblAgreeThat = new Label(composite, SWT.WRAP);
		lblAgreeThat.setBounds(71, 298, 511, 110);
		lblAgreeThat.setText("无论在何种情况下，本人同意负责对付以上的账目。\nAGREE THAT MY LIABILITY FOR composite BILL TS NOT WAIVED ANDAGREE TO BE HELD PERSONALLY LIABLE IN THE EVENT THAT\nINDICATED PERSON COMPANY OR ASSOCIATION FAILS TO PAY FOR ANYPART OR THE FULL AMOUNT OF THESE CHARGES\n");

		Label lblNewLabel_4 = new Label(composite, SWT.NONE);
		lblNewLabel_4.setBounds(785, 324, 91, 25);
		lblNewLabel_4.setText("宾客签字：");

		lblAgreeThat.setImage(SWTResourceManager.getImage(Login.class, "/com/yc/images/登记入住界面1.png"));
		SWTUtil.imagetSize(lblAgreeThat);
		lblNewLabel_4.setImage(SWTResourceManager.getImage(Login.class, "/com/yc/images/登记入住界面2.png"));
		SWTUtil.imagetSize(lblNewLabel_4);

		text_7 = new Text(composite, SWT.BORDER);
		text_7.setBounds(782, 382, 124, 26);

		Label lblNewLabel_5_1 = new Label(composite, SWT.NONE);
		lblNewLabel_5_1.setText("押金：");
		lblNewLabel_5_1.setBounds(71, 235, 76, 20);

		text_8 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_8.setBounds(187, 232, 173, 26);

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.setImage(SWTResourceManager.getImage(Login.class, "/com/yc/images/登记入住界面按钮2.PNG"));
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// 房间号
				String r_id = text_1.getText().trim();
				// 身份证
				String c_cardid = text_2.getText().trim();
				// 入住时间
				String date = text_4.getText().trim();

				if(StringUtil.checkNull(r_id, c_cardid, date)) {
					StringUtil.showMessageBox(getShell(), "信息不完整，请补全信息再登记");
					return ;
				}

				SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
				Date date1 = new Date(System.currentTimeMillis()); // 当前日期
				LoginIDao dao = new LoginDao();
				Map<String, Object> map1 = dao.SelectaDvanceRoom(c_cardid);
				try {
					Date date2 = formatter.parse(map1.get("start_date").toString());
					if (date1.before(date2)) { // 选择的日期比当前日期小
						MessageBox messageBox = new MessageBox(getShell(), SWT.OK | SWT.CANCEL);
						messageBox.setMessage("当前日期不是您预定的日期，是否提前入住");
						if(messageBox.open() == 32) {
							dao.AlterDate(c_cardid, date);
						}else {
							return ;
						}
					}
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				int re = 0;
				re = dao.Entry(r_id, c_cardid, dateString);
				if(re == 0) {
					StringUtil.showMessageBox(getShell(), "入住登记失败");
					return ;
				}else if(re == -1) {
					StringUtil.showMessageBox(getShell(), "已经登记入住，不能重复登记");
					return ;
				}else {
					StringUtil.showMessageBox(getShell(), "入住登记成功");
					l_id = dao.SelectId(date).get("l_id").toString();
					
				}
			}
		});
		btnNewButton_1.setBounds(380, 541, 98, 30);
		btnNewButton_1.setText("登记入住");

		Button button = new Button(composite, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(MailUtils.encode("登记房间","C:\\Users\\16389\\Desktop\\二维码" + 1 + ".jpg")) {
					label_3.setImage(SWTResourceManager.getImage("C:\\Users\\16389\\Desktop\\二维码" + 1 + ".jpg"));
					SWTUtil.imageSize(label_3);
				}

			}
		});
		button.setImage(SWTResourceManager.getImage(Login.class, "/com/yc/images/登记入住界面按钮1.PNG"));
		button.setBounds(412, 453, 98, 30);
		button.setText("生成二维码");

		label_3 = new Label(composite, SWT.NONE);
		label_3.setBounds(548, 433, 129, 115);

		Borider(map.get("c_cardid").toString());

	}

	static void Borider(String c_cardid) {
		LoginIDao dao = new LoginDao();
		Map<String, Object> mapRoom = new HashMap<String, Object>();
		mapRoom = dao.SelectaDvanceRoom(c_cardid);
		if(mapRoom != null) {
			// 房间号
			String r_id = mapRoom.get("r_id").toString();
			text_1.setText(r_id);

			// 押金
			String r_price = mapRoom.get("r_price").toString();
			text_8.setText(r_price);

			// 房间类型
			String r_type = mapRoom.get("r_type").toString();
			text_5.setText(r_type);

			// 房间价格
			String price = mapRoom.get("price").toString();
			text_6.setText(price);
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
